﻿using System;
using System.ComponentModel;
using System.Disposables;
using System.Linq.Expressions;
using System.Windows;
using SoftSource.Common;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.ViewModels
{
    public sealed class UserEditorViewModel : IUserEditorViewModel
    {
        #region Fields
        private CompositeDisposable compositeDisposable;
        #endregion Fields

        public UserEditorViewModel()
        {
            this.compositeDisposable = new CompositeDisposable();
        }

        #region IUserEditorViewModelConsume Members

        private User user;
        public User User
        {
            get { return this.user; }
            set
            {
                this.user = value;
                this.PropertyChanged.Notify(() => this.User);
            }
        }

        public Func<Action, UIElement> CreateUserProjectEditor { get; set; }

        public IObservable<Unit> Load { get; set; }

        #endregion

        #region IEditor Members

        public Action Cancel { get; set; }
        public IObservable<Unit> Save { get; set; }
        public IObservable<Unit> Apply { get; set; }
        public Action<Exception> Failure { get; set; }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            this.compositeDisposable.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion

        ~UserEditorViewModel()
        {
            this.Dispose();
        }
    }
}
